package com.dcbeida.activity.salsesdedail;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.activity.zyff.ZyWithGaDetailPresenter;
import com.dcbeida.bean.MyClientListBean;
import com.dcbeida.bean.Patient;
import com.dcbeida.bean.PatientList;
import com.dcbeida.bean.SalsesDetail;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.model.SotresalesModel;
import com.dcbeida.utils.JsonGet;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 150410 on 2017/10/19.
 */

public class SalsesDetailPresenter implements SalsesDetailContract.Present {

    private final ModelUtils model;
    private SalsesDetailContract.View view;
    private Activity act;
    private SotresalesModel sotresalesModel;

    public SalsesDetailPresenter(SalsesDetailContract.View view, Activity act) {
        this.view = view;
        model = ModelUtils.newInstance();
        this.act = act;
        sotresalesModel = SotresalesModel.newInstance();
    }


    @Override
    public void start() {

    }


    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };

    //MyApplication.user.getUid()
    @Override
    public void getSalsesDetailInfo(String uid, String orderid, String patientid) {
        JsonGet jg = new JsonGet().putopt("uid", uid)
                .putopt("orderid", orderid)
                .putopt("patientid", patientid);
        String json = jg.tostring();

        model.getDate("getorderdrugs", json, new SalsesDetailPresenter.GetListCallBack(act, SalsesDetail.class, lis));
    }

    @Override
    public void getList(int pageindex, int pagesize, String truename, String idcardnum, String hospitalid, String patientid) {
        model.getDate("getpatientlist", new JsonGet()
                .putopt("uid", MyApplication.user.getUid())
                .putopt("shopid", MyApplication.user.getShopid())
                .putopt("pageindex", pageindex)
                .putopt("pagesize", pagesize)
                .putopt("truename", truename)
                .putopt("idcardnum", idcardnum)
                .putopt("hospitalid", hospitalid)
                .putopt("patientid", patientid)
                .tostring(), new GetList2CallBack(act, MyClientListBean.class, lis));
    }

    public class GetListCallBack<T> extends DialogCallback<T> {
        public GetListCallBack(Activity activity, Type type, DialogInterface.OnCancelListener lis) {
            super(activity, type, lis);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((SalsesDetail) data).getList().size() >= 0) {
                    view.showSalsesDetailList(((SalsesDetail) data).getList());
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");
        }
    }

    public class GetList2CallBack<T> extends DialogCallback<T> {
        public GetList2CallBack(Activity activity, Type type, DialogInterface.OnCancelListener lis) {
            super(activity, type, lis);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((MyClientListBean) data).getList().size() >= 0) {
                    view.showList(((MyClientListBean) data).getList());
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
        }
    }

    @Override
    public void queryPatient(int patientid) {
        sotresalesModel.querypatient(4,patientid+"",new QuerypatientCallBack(act, PatientList.class,lis));
    }

    //药店销售-查询患者信息列表
    class QuerypatientCallBack<T> extends DialogCallback<T> {

        public QuerypatientCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof PatientList) {
                    if (((PatientList) data).getList().size() > 0) {
                        Patient patient = ((PatientList) data).getList().get(0);
                        view.showSignStatus(patient.getSignstatus());
                    } else {
                        view.showTip("未查询到患者信息...");
                    }
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }

}
